#include "rectitem.h"
#include <QCursor>
#include <QPen>
#include <QGraphicsSceneHoverEvent>

RectItem::RectItem(QGraphicsItem *parent) :
    QGraphicsRectItem(parent)
{
    setFlag(ItemIsMovable);
    setFlag(ItemIsSelectable);
    setFlag(ItemSendsGeometryChanges);
    setAcceptHoverEvents(true);
    setPen(QPen(Qt::red,3));
}

void RectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if(cursor().shape() == Qt::ArrowCursor){
        QGraphicsRectItem::mouseMoveEvent(event);
    }else if(cursor().shape() == Qt::SizeFDiagCursor){
        QRectF rc = rect();
        if(event->pos().x() > rc.left() && event->pos().y() > rc.top()){
            rc.setBottomRight(event->pos());
            setRect(rc);
        }
    }
}

void RectItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    // 右下角光标形状
    int cursorW = 4;
    QRectF bottomRight(rect().bottomRight()-QPointF(cursorW,cursorW),
                       rect().bottomRight()+QPointF(cursorW,cursorW));
    if(bottomRight.contains(event->pos())){
        setCursor(Qt::SizeFDiagCursor);
    }else{
        setCursor(Qt::ArrowCursor);
    }
}
